package org.vectrics.common.html;

import java.awt.Color;
import java.lang.Integer;

public class ColorUtil {

    public static String makeHtmlColorString(int redBg, int greenBg, int blueBg) {
        String colorString = "";
        if (redBg > 255)
            redBg = 255;
        if (greenBg > 255)
            greenBg = 255;
        if (blueBg > 255)
            blueBg = 255;
        String redStr = Integer.toHexString(redBg);
        String greenStr = Integer.toHexString(greenBg);
        String blueStr = Integer.toHexString(blueBg);
        
        while (redStr.length() < 2)
            redStr = "0" + redStr;
        while (blueStr.length() < 2)
            blueStr = "0" + blueStr;
        while (greenStr.length() < 2)
            greenStr = "0" + greenStr;
            
        colorString = "#" + redStr + greenStr + blueStr;
        return(colorString);
    }
    
    
    
    public static Color htmlStringToColor(String htmlColor) {
        Color color = null;
        Integer red = null;
        Integer green = null;
        Integer blue = null;
        if (htmlColor.startsWith("#")) {
            String redHex = htmlColor.substring(1, 3);
            red = Integer.decode("#" + redHex);
            String greenHex = htmlColor.substring(3, 5);
            green = Integer.decode("#" + greenHex);
            String blueHex = htmlColor.substring(5, 7);
            blue = Integer.decode("#" + blueHex);
        }
        color = new Color(red.intValue(), green.intValue(), blue.intValue());
        return(color);
    }
}
